package me.zhengjie.pm.projectplan.service.impl;

import me.zhengjie.pm.projectplan.domain.ProjectPlan;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.pm.projectplan.repository.ProjectPlanRepository;
import me.zhengjie.pm.projectplan.service.ProjectPlanService;
import me.zhengjie.pm.projectplan.service.dto.ProjectPlanDto;
import me.zhengjie.pm.projectplan.service.dto.ProjectPlanQueryCriteria;
import me.zhengjie.pm.projectplan.service.mapper.ProjectPlanMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author ZGZ
* @date 2020-04-25
*/
@Service
//@CacheConfig(cacheNames = "projectPlan")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ProjectPlanServiceImpl implements ProjectPlanService {

    private final ProjectPlanRepository projectPlanRepository;

    private final ProjectPlanMapper projectPlanMapper;

    public ProjectPlanServiceImpl(ProjectPlanRepository projectPlanRepository, ProjectPlanMapper projectPlanMapper) {
        this.projectPlanRepository = projectPlanRepository;
        this.projectPlanMapper = projectPlanMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(ProjectPlanQueryCriteria criteria, Pageable pageable){
        Page<ProjectPlan> page = projectPlanRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(projectPlanMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<ProjectPlanDto> queryAll(ProjectPlanQueryCriteria criteria){
        return projectPlanMapper.toDto(projectPlanRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public ProjectPlanDto findById(Integer id) {
        ProjectPlan projectPlan = projectPlanRepository.findById(id).orElseGet(ProjectPlan::new);
        ValidationUtil.isNull(projectPlan.getId(),"ProjectPlan","id",id);
        return projectPlanMapper.toDto(projectPlan);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ProjectPlanDto create(ProjectPlan resources) {
        return projectPlanMapper.toDto(projectPlanRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(ProjectPlan resources) {
        ProjectPlan projectPlan = projectPlanRepository.findById(resources.getId()).orElseGet(ProjectPlan::new);
        ValidationUtil.isNull( projectPlan.getId(),"ProjectPlan","id",resources.getId());
        projectPlan.copy(resources);
        projectPlanRepository.save(projectPlan);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            projectPlanRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ProjectPlanDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ProjectPlanDto projectPlan : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("项目", projectPlan.getProjectId());
            map.put("重要程度", projectPlan.getImportance());
            map.put("主要完成形式/成果", projectPlan.getAchievement());
            map.put("计划开始时间", projectPlan.getPlanStartTime());
            map.put("计划完成时间", projectPlan.getPlanFinishTime());
            map.put("实际开始时间", projectPlan.getStartTime());
            map.put("实际完成时间", projectPlan.getFinishTime());
            map.put("部门项目工作文档位置", projectPlan.getDocPath());
            map.put("计划依据", projectPlan.getPlanBasis());
            map.put("项目密级", projectPlan.getSecretLevel());
            map.put("研制内容", projectPlan.getContents());
            map.put("部门项目管理文档位置", projectPlan.getMngDocPath());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}